## ---------------------------
##
## "How COVID-19 Displaced Climate Change: Mediated Climate Change Activism and Issue Attention in the Swiss Media and Online Sphere"
##
## Main results of the paper can be reproduced with the code in this script
##
## Author: Adrian Rauchfleisch, Dario Siegen, & Daniel Vogler
##
## Date Created: 2021-11-16
##
## 
## Email: adrian.rauchfleisch@gmail.com
##
## ---------------------------

# files 
# the data for the models is in the file data.RData
load("data.RData")
# hashtag co-occurrence files available in the repository:
# hashtag_2020.graphml
# hashtag_2019.graphml

library(zoo)
library(tseries)
library(CausalImpact)
library(ggplot2)
library(stringr)
library(igraph)
library(semnet)


# Models and Figure 2 ####
# climate change media attention
ts_media_climate <- zoo(media_attention$count, media_attention$date)
adf.test(ts_media_climate, alternative = "stationary")
set.seed(12)
ts_causal_media_climate <- CausalImpact(ts_media_climate, 
                                pre.period= as.Date(c("2019-04-01", "2020-01-31")), 
                                post.period=as.Date(c("2020-02-01", "2020-10-31")), 
                                # we have weekly seasonality
                                model.args = list(nseasons = 7, season.duration = 1) )

ts_causal_media_climate

jpeg("ts_plot_climate_media.jpeg", width = 10, height = 5, units = 'in', res = 800)
plot(ts_causal_media_climate) + theme_minimal() + ggtitle("Media")
dev.off()

# climate change Twitter attention
ts_twit_climate <- zoo(twitter_attention$count, twitter_attention$date)
adf.test(ts_twit_climate, alternative = "stationary")
set.seed(12)
ts_causal_twitter_climate <- CausalImpact(tl_zoo_twitter, 
                                 pre.period= as.Date(c("2019-04-01", "2020-01-31")),
                                 post.period=as.Date(c("2020-02-01", "2020-10-31")),
                                 # we have weekly seasonality
                                 model.args = list(nseasons = 7, season.duration = 1) )

ts_causal_twitter_climate

jpeg("ts_plot_climate_twitter.jpeg", width = 10, height = 5, units = 'in', res = 800)
plot(ts_causal_twitter_climate) + theme_minimal() + ggtitle("Twitter")
dev.off()

# Hashtag co-occurrence ####
# code is here for reference
# the graphml files are available in the repository
# we used the same code for the tweets in 2019 and 2020 


hashtags <- str_extract_all(cc_tweets_2019$text, "#\\w+")
hashtags <- lapply(hashtags, tolower)
hashtags <- lapply(hashtags, unique)

# count hashtags
hashtags_table <- as.data.frame(table(unlist(hashtags)), stringsAsFactors = F)
hashtags_table <- hashtags_table[order(hashtags_table$Freq, decreasing = T), ]

# make co-occurrence edge list
pairs_list <- list()
for( i in 1:length(hashtags)) {
  # if only one hashtag, jump to next
  if(length(hashtags[[i]])<2) {
    next
  }
  pairs_list[[i]] <- as.data.frame(t(combn(hashtags[[i]], m=2)), stringsAsFactors = F)
}

co_edges <- as.data.frame( rbindlist(pairs_list), stringsAsFactors=F)
colnames(co_edges) <- c("source","target")

# we only analyze the top 100 hashtags
co_edges <- co_edges[co_edges$source %in% hashtags_table$Var1[1:100] & co_edges$target %in% hashtags_table$Var1[1:100], ]

# not directed as we have a co-occurrence network
text_network <- graph.edgelist(as.matrix(co_edges), directed = F)
# we need to add weights
text_network <- graph.adjacency(get.adjacency(text_network), weighted=TRUE, mode = "undirected")

# extract backbone
text_backbone <- getBackboneNetwork(text_network, alpha = 0.05, direction = "none",
                                    delete.isolates = T, max.vertices = NULL, use.original.alpha = T,
                                    k.is.Nvertices = F)
# community detection with Louvain
V(text_backbone)$community <- membership(cluster_louvain(text_backbone)) 
write_graph(text_backbone, "hashtag_2019.graphml", format="graphml")

# extract hashtag list
hashtags_table$community <- V(text_backbone)$community[match(hashtags_table$Var1, V(text_backbone)$name)]
write.csv(hashtags_table, file="hashtags_table_2019.csv", row.names=F)

# Appendix 2: Brexit analysis ####
# Brexit media
ts_media_brexit <- zoo(brexit_media_attention$count, brexit_media_attention$date)
adf.test(ts_media_brexit, alternative = "stationary")

ts_causal_media_brexit <- CausalImpact(ts_media_brexit, 
                                       pre.period= as.Date(c("2019-04-01", "2020-01-31")),
                                       post.period=as.Date(c("2020-02-01", "2020-10-31")),
                                       # we have weekly seasonality
                                       model.args = list(nseasons = 7, season.duration = 1) )

jpeg("ts_plot_brexit_media.jpeg", width = 10, height = 5, units = 'in', res = 800)
plot(tl_causal_media_brexit) + theme_minimal()
dev.off()

tl_causal_media_brexit

# Brexit Twitter
ts_twit_brexit <- zoo(brexit_twit_attention$count, brexit_twit_attention$date)
adf.test(ts_twit_brexit, alternative = "stationary")

ts_causal_twit_brexit <- CausalImpact(ts_twit_brexit, 
                                        pre.period= as.Date(c("2019-04-01", "2020-01-31")),
                                        post.period=as.Date(c("2020-02-01", "2020-10-31")),
                                        # we have weekly seasonality
                                        model.args = list(nseasons = 7, season.duration = 1) )

jpeg("ts_plot_brexit_twit.jpeg", width = 10, height = 5, units = 'in', res = 800)
plot(ts_causal_twit_brexit) + theme_minimal()
dev.off()

tl_causal_media_brexit


